<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="商品名称" prop="goodName">
        <el-input
          v-model="queryParams.goodName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品状态" prop="goodStatus">
        <el-select v-model="queryParams.goodStatus" placeholder="请选择商品状态" clearable>
          <el-option
            v-for="dict in dict.type.is_up"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否热卖" prop="hotFlag">
        <el-select v-model="queryParams.hotFlag" placeholder="请选择是否热卖" clearable>
          <el-option
            v-for="dict in dict.type.is_or_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否推荐" prop="recommendFlag">
        <el-select v-model="queryParams.recommendFlag" placeholder="请选择是否推荐" clearable>
          <el-option
            v-for="dict in dict.type.is_or_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="新品推荐" prop="newFlag">
        <el-select v-model="queryParams.newFlag" placeholder="请选择是否新品推荐" clearable>
          <el-option
            v-for="dict in dict.type.is_or_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['kakabl:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['kakabl:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['kakabl:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kakabl:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品ID" align="center" prop="id" />
<!--      <el-table-column label="商品ID" align="center" prop="goodId" />-->
      <el-table-column label="商品名称" align="center" prop="goodName" />
<!--      <el-table-column label="商品图片" align="center" prop="goodImageJson" />-->
      <el-table-column label="商品数量" align="center" prop="goodNums" />
      <el-table-column label="开售时间" align="center" prop="goodStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.goodStartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品总量" align="center" prop="goodNumsAll" />
      <el-table-column label="价格" align="center" prop="goodPrice" />
      <el-table-column label="产品作者" align="center" prop="goodAuthor" />
      <el-table-column label="商品状态" align="center" prop="goodStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_up" :value="scope.row.goodStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="余额数" align="center" prop="goodNum" />
      <el-table-column label="是否热卖" align="center" prop="hotFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_or_no" :value="scope.row.hotFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="是否推荐" align="center" prop="recommendFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_or_no" :value="scope.row.recommendFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="新品推荐" align="center" prop="newFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_or_no" :value="scope.row.newFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column fixed="right"  label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['kakabl:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kakabl:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改卡卡-商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品ID" prop="goodId">
          <el-input v-model="form.goodId" placeholder="请输入商品ID" />
        </el-form-item>
<!--        <el-form-item label="商品分类ID" prop="categorizeId">-->
<!--          <el-input v-model="form.categorizeId" placeholder="请输入商品分类ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="商品名称" prop="goodName">
          <el-input v-model="form.goodName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品图片" prop="goodImageJson">
          <el-input v-model="form.goodImageJson" placeholder="请输入商品图片" />
        </el-form-item>
        <el-form-item label="商品数量" prop="goodNums">
          <el-input v-model="form.goodNums" placeholder="请输入商品数量" />
        </el-form-item>
        <el-form-item label="开售时间" prop="goodStartTime">
          <el-date-picker clearable
            v-model="form.goodStartTime"
            type="date"
            value-format="yyyy-MM-dd hh:mm:ss"
            placeholder="请选择开售时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="价格" prop="goodPrice">
          <el-input v-model="form.goodPrice" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="产品作者" prop="goodAuthor">
          <el-input v-model="form.goodAuthor" placeholder="请输入产品作者" />
        </el-form-item>
        <el-form-item label="产品介绍" prop="goodDesc">
          <el-input v-model="form.goodDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品状态" prop="goodStatus">
          <el-select v-model="form.goodStatus" placeholder="请选择商品状态">
            <el-option
              v-for="dict in dict.type.is_up"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="余额数" prop="goodNum">
          <el-input v-model="form.goodNum" placeholder="请输入余额数" />
        </el-form-item>
        <el-form-item label="是否热卖" prop="hotFlag">
          <el-select v-model="form.hotFlag" placeholder="请选择是否热卖">
            <el-option
              v-for="dict in dict.type.is_or_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否推荐" prop="recommendFlag">
          <el-select v-model="form.recommendFlag" placeholder="请选择是否推荐">
            <el-option
              v-for="dict in dict.type.is_or_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="新品推荐" prop="newFlag">
          <el-select v-model="form.newFlag" placeholder="请选择是否新品推荐">
            <el-option
              v-for="dict in dict.type.is_or_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/kakabl/goods";

export default {
  name: "Goods",
  dicts: ['is_or_no', 'is_up'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 卡卡-商品表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodId: null,
        categorizeId: null,
        goodName: null,
        goodImageJson: null,
        goodNums: null,
        goodStartTime: null,
        goodNumsAll: null,
        goodPrice: null,
        goodAuthor: null,
        goodDesc: null,
        goodStatus: null,
        goodNum: null,
        hotFlag: null,
        recommendFlag: null,
        newFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        goodNums: [
          { required: true, message: "商品数量不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询卡卡-商品列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodId: null,
        categorizeId: null,
        goodName: null,
        goodImageJson: null,
        goodNums: null,
        goodStartTime: null,
        goodNumsAll: null,
        goodPrice: null,
        goodAuthor: null,
        goodDesc: null,
        goodStatus: null,
        goodNum: null,
        hotFlag: null,
        recommendFlag: null,
        newFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加卡卡-商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改卡卡-商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除卡卡-商品编号为"' + ids + '"的数据项？').then(function() {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('kakabl/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
